#include "ls1x.h"
#include "ls1x_gpio.h"
#include "UserGpio.h"
#include "Config.h"
#include "LED.h"
#include "ls1x_latimer.h"
#include "KEY.h"
#include "BEEP.h"
#include "oled.h"
#include "fire.h"
#include "oledpic.h"
#include "dht11.h"
#include "RGB_LED.h"
#include "fan.h"
#include "queue.h"
#include "ls1x_uart.h"
#include "ls1x_common.h"
#include "fumes.h"
#include "ls1c102_ptimer.h"

#define LED 20
char arr[30];
extern  uint8_t MQTT_UP_data[8];
uint16_t  rh_threshold=500;
char str[30];
static uint8_t fire;
uint16_t  temp,humi;
uint8_t received_data = 0;
uint8_t key_flag;
uint8_t oled_flag=0;
static uint16_t reliability;
static uint16_t maxValue;
 //温湿度数据上云平台    数据包


uint8_t data[6];
 int fan_flag=0;
uint8_t Read_Buffer[255]; // 设置接收缓冲数组
uint8_t Read_length;
 int red_flag=0;
 int uart_temp_flag=0;
 int temp_temp;
int smoke_temp;
int pwm_cmp=10;     // 电机的pwm波的输入大小
int motor_rp=0;    // 表示电机的转动方向：0为反转，1为正转

unsigned char x=0,y=0;
uint32_t counter = 0;
int16_t v;
int16_t r = 0;
int t;
uint8_t flag = 0;
 //extern  uint8_t Read_Buffer[255];
int main(int arg, char *args[])
{
    SystemClockInit(); // 时钟等系统配置
    GPIOInit(); // io配置
    LED_init();
    KEY_init();
    OLED_Init();
    while(DHT11_Init());
    RGB_LED_Init();
    fan_Init();
    Smoke_Init();
   
   
   
//    int num=0;
//    OLED_DrawBMP(5,0,53,7,house);
    int num;
    EnableInt(); // 开总中断
    Timer_Init(3000);
    Queue_Init(&Circular_queue);
    Uart1_init(9600);
    Uart0_init(9600);
    while (1)
    {   
        // if (Queue_isEmpty(&Circular_queue) == 0) // 判断队列是否为空，即判断是否收到数据
        //      {
        //         Read_length = Queue_HadUse(&Circular_queue); // 返回队列中数据的长度
        //         Queue_Read(&Circular_queue, Read_Buffer, Read_length); // 读取队列缓冲区的值到接收缓冲区
        //         OLED_Clear();
                //接收温湿度值并发送
                DHT11_Read_Data(&temp,&humi);
                temp/=10;
                humi/=10;
                // OLED_DrawBMP(0,0,20,7,house);
                // sprintf(str,"Temp=%2d ℃",temp);
                // OLED_Show_Str(17,0,str,16);
                // sprintf(str,"Humi=%2d \%RH",humi);
                // OLED_Show_Str(17,2,str,16);
                temp_temp=temp+5;
                uint8_t uart_arr[4] = {0xF4,0xF5,temp_temp,0xFB};
                if(uart_temp_flag){
                    delay_ms(200);
                    UART_SendDataALL(UART0,uart_arr,4);
                    LED3_CTRL(1);
                    
                    uart_temp_flag=0;
                }else{
                    LED3_CTRL(0);
                }
                LED1_CTRL(1);
                delay_ms(500);
                LED1_CTRL(0);

                //烟雾浓度检测，浓度大于100时打开风扇,蜂鸣器报警，LED灯全部亮起
                Smoke_Read_Data(&smoke_temp);
                sprintf(arr,"fumes=%2d",smoke_temp);
                //OLED_Clear();
                
                if(smoke_temp>100){//烟雾浓度值大于一定值时警报
                    BEEP_ON;
                    FAN_ON;
                    LED1_CTRL(1);
                    LED2_CTRL(1);
                    LED3_CTRL(1);
                    LED4_CTRL(1);
                } else{
                    BEEP_OFF;
                    FAN_OFF;
                    LED1_CTRL(0);
                    LED2_CTRL(0);
                    LED3_CTRL(0);
                    LED4_CTRL(0);
                } 
                key_flag=KEY_Scan();
                switch(key_flag){
                    case 1: oled_flag=1;    break;
                    case 2: oled_flag=2;    break;
                    case 3: fan_flag;       break;
                    case 4: red_flag++;     break;
                }
                if(oled_flag==1){
                    OLED_Clear();
                    OLED_DrawBMP(0,0,33,4,house);
                    sprintf(str,"Humi=%2d\%RH",humi);
                    OLED_Show_Str(34,2,str,6);
                    sprintf(str,"Temp=%2d℃",temp);
                    OLED_Show_Str(34,0,str,6);
                    
                    OLED_Show_Str(34,4,arr,6);
                    sprintf(str, "fire= %d", fire);
                    OLED_Show_Str(34, 6, str, 6);    // OLED显示界面
                }else if(oled_flag==2){
                    OLED_Clear();
                    OLED_Show_Str(5,0,"2025：7月3号",10);
                    OLED_Show_Str(5,2,"2025：7月10号",10);
                   
                }
                fire=FIRE_Read_Bit();                      // 接受火焰状态

                data[0] = 3;
                data[1] = fire % 256;
             
                if(fire==1)         // 当检测到火焰发出警报
                {
                gpio_write_pin(LED, 1);
                BEEP_ON;
                }
                else
                {
                gpio_write_pin(LED, 0);
                BEEP_OFF;
            }
        delay_ms(200);
                if(fan_flag==2) fan_flag=0;
                if(fan_flag) FAN_ON;
                else FAN_OFF;
                switch(red_flag){
                    case 1:RED_CTRL(0); GREEN_CTRL(0);BULE_CTRL(1); break;
                    case 2:RED_CTRL(0); GREEN_CTRL(1);BULE_CTRL(0); break;
                    case 3:RED_CTRL(1); GREEN_CTRL(0);BULE_CTRL(0); break;
                    case 4:RED_CTRL(1); GREEN_CTRL(0);BULE_CTRL(1); break;
                    case 5:RED_CTRL(1); GREEN_CTRL(1);BULE_CTRL(0); break;
                    case 6:RED_CTRL(1); GREEN_CTRL(1);BULE_CTRL(1); break;
                    case 7:RED_CTRL(0); GREEN_CTRL(0);BULE_CTRL(0); break;
                }
                if(red_flag==7) red_flag=0;
                //pwm_cmp=pid1(v,20);               // 电机PID算法，预定转速为20r/s
//pwm_cmp=pid1(v,20);               // 电机PID算法，预定转速为20r/s

        // 计算可靠性
        if(flag == 0)
        {
            maxValue = (temp/10) > (humi/10)? (temp/10) : (humi/10);
            if(maxValue < smoke_temp)
                maxValue = smoke_temp;
            if(maxValue!=0)
                flag = 1;
        }
        if(flag == 1)
        {
            if(temp < 450)
            {
                uint16_t nor_temp = (temp * 5) / maxValue;
                uint16_t nor_humi = ((humi - 250) * 3) / maxValue;
                uint16_t nor_air = (smoke_temp * 2) / maxValue;
                uint16_t nor_max = ((1000 - 250) * 3 + smoke_temp* 2 + 200 * 5) / maxValue;
                reliability = (nor_max + nor_temp - nor_humi - nor_air) / 10;
            }
            else{
                uint16_t nor_temp = ((temp - 300) * 5) / maxValue;
                uint16_t nor_humi = ((humi - 250) * 3) / maxValue;
                uint16_t nor_air = (smoke_temp * 2) / maxValue;
                uint16_t nor_max = ((1000 - 250) * 3 + smoke_temp * 2 + (500 - 300) * 5) / maxValue;
                reliability = (nor_max - nor_temp - nor_humi - nor_air) / 10;
            }
        }
        
        
    }
}